import 'package:fish_redux/fish_redux.dart';
import 'package:flutter/material.dart' hide Action;
import 'package:hayll_app_village_master/hayll/network/yll_network.dart';
import 'package:hayll_app_village_master/hayll/village_manager/home/village_grid_component/bean/village_grid_response.dart';
import 'package:hayll_app_village_master/hayll/village_manager/network/village_action.dart';
import 'action.dart';
import 'state.dart';

Effect<VillageSimpleState> buildEffect() {
  return combineEffects(<Object, Effect<VillageSimpleState>>{
    Lifecycle.initState: _initState,
    VillageSimpleAction.action: _onAction,
    VillageSimpleAction.initUI: _initUI,
    VillageSimpleAction.openHouseChoose: _openHouseChoose,
  });
}

void _onAction(Action action, Context<VillageSimpleState> ctx) {}

void _initUI(Action action, Context<VillageSimpleState> ctx) {
  ctx.dispatch(VillageSimpleActionCreator.refreshUI());
}

void _initState(Action action, Context<VillageSimpleState> ctx) {
  ctx.dispatch(VillageSimpleActionCreator.refreshUI());
}

void _requestVillageHouseGrid(Action action, Context<VillageSimpleState> ctx) {
  // var params = {'villageId': 118};
  // YllNetwork.requestPost(VillageAction.getVillageGrid, params: params,
  //     success: (json) {
  //   ctx.state.villageGrid = VillageGridResult.fromJson(json);
  //   ctx.dispatch(VillageGridActionCreator.refreshUI());
  // }, failed: (errorCode, errorMsg) {
  //   Fluttertoast.showToast(msg: '$errorCode,$errorMsg');
  // });
}

void _openHouseChoose(Action action, Context<VillageSimpleState> ctx) async {
  var result = await Navigator.pushNamed(ctx.context, '/chooseHouse');
  print('收到的值:${result}');
  if (result != null && result is Map) {
    var title = '';
    var build = result['build'];
    var unit = result['unit'];
    var house = result['house'];
    if (build != null && build is VillageBuildGridResult) {
      title += build.buildingName + ' > ';
    }
    if (unit != null && unit is VillageUnitGridResult) {
      title += unit.itemName + ' > ';
    }
    if (house != null && house is VillageHouseGridResult) {
      title += house.roomName;
    }
    ctx.state.build = build;
    ctx.state.unit = unit;
    ctx.state.house = house;
    ctx.state.houseNaviString = title;
    ctx.dispatch(VillageSimpleActionCreator.refreshUI());
    ctx.dispatch(VillageSimpleActionCreator.chooseHouseChanged());
  }
}
